package com.aigc.service.impl;

import com.aigc.domin.ChatHistory;
import com.aigc.mapper.ChatHistoryMapper;
import com.aigc.service.ChatHistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ChatHistoryServiceImpl extends ServiceImpl<ChatHistoryMapper, ChatHistory> implements ChatHistoryService {
    @Override
    public void addChatHistory(String type, String conversationId) {
        Long count = lambdaQuery().eq(ChatHistory::getConversationId, conversationId)
                .count();
        if(count > 0){
            return;
        }
        ChatHistory chatHistory = ChatHistory.builder()
                .type(type)
                .userId(null)
                .conversationId(conversationId)
                .title(type+conversationId)
                .createTime(LocalDateTime.now())
                .build();
        this.save(chatHistory);
    }

    @Override
    public List<String> getChatHistory(Long userId,String type) {
        return  lambdaQuery().eq(userId != null,ChatHistory::getUserId, userId)
                .eq(ChatHistory::getType, type)
                .list().stream().map(ChatHistory::getConversationId).toList();
    }

}
